<?php
/**
 * AdroitBaseDB Class
 *
 * This class is used to manage a MySQL database connection and encapsulates common database functions.
 * 
 * @package adroit
 * @subpackage db
 */
class AdroitBaseDB {
	
	public $connection ;
	
	public $affected_rows ;
	
	public $insert_id ;
	
	/**
	 * DbaseMysql Constructor
	 *
	 * Runs the connectToDatabase method.
	 *
	 * @param string $database_name The name of the database to be connected to.
	 * @param string $host The host where the database resides.  (ie localhost)
	 * @param string $username The username to connect with.
	 * @param string $password The password associated with username.
	 * @param string $port The port to connect to.  If left blank the default port is used.
	 */
	public function AdroitBaseDB($database_name , $host , $username , $password , $port) {
		$this->connectToDatabase($database_name, $host, $username, $password, $port) ;
	}
	
	/**
	 * connectToDatabase
	 *
	 * Connects to a MySQL database and saves the connection to the connection object property.
	 * This method returns nothing.
	 *
	 * @param string $database_name The name of the database to be connected to.
	 * @param string $host The host where the database resides.  (ie localhost)
	 * @param string $username The username to connect with.
	 * @param string $password The password associated with username.
	 * @param string $port The port to connect to.  If left blank the default port is used.
	 */
	public function connectToDatabase($database_name , $host , $username , $password , $port) {
		$this->connection = @mysql_connect($host . ($port != '' ? ':' . $port : ''), $username, $password) ;
		$this->errorCheck();
		$this->selectDatabase($database_name) ;
	}
	
	/**
	 * errorCheck
	 *
	 * This method checks to see if an error has occurred on the MySQL Database connection.  If so
	 * a AdroitDBException is thrown containing the error text.  This method returns nothing.
	 */
	public function errorCheck() {
		if ($this->connection) {
			if (mysql_error($this->connection)) {
				throw new AdroitDBException("Dbase Error: " . mysql_error($this->connection)) ;
			}
		} else {
			throw new AdroitDBException("No connection created") ;
		}
	}
	
	public function fetchAssocArray($query , $array_index = '') {
		$query_result = mysql_query($query, $this->connection);
		$return_array = Array();
		while($row = mysql_fetch_assoc($query_result)) {
			if ($array_index != '' && isset ( $row [ $array_index ] )) {
				$return_array [ $row [ $array_index ] ] = $row ;
			} else {
				$return_array [] = $row ;
			}
		}
		$this->errorCheck () ;
		return $return_array ;
	}
	
	public function fetchAssocRow ( $query ) {
		$query_result = mysql_query ( $query, $this->connection ) ;
		$this->errorCheck() ;
		if ($row = mysql_fetch_assoc ( $query_result )) {
			$this->errorCheck() ;
			return $row ;
		} else {
			$this->errorCheck() ;
			return false ;
		}
	}
	
	public function fetchCol ( $query ) {
		$query_result = mysql_query ( $query, $this->connection ) ;
		$return_array = Array ( ) ;
		while ( $row = mysql_fetch_array ( $query_result, MYSQL_NUM ) ) {
			if (count ( $row ) == 1) {
				$return_array [] = $row [ 0 ] ;
			} else {
				$return_array [ $row [ 0 ] ] = $row [ 1 ] ;
			}
		}
		$this->errorCheck () ;
		return $return_array ;
	}
	
	public function fetchValue ( $query ) {
		$query_result = mysql_query ( $query, $this->connection ) ;
		$this->errorCheck () ;
		if ($row = mysql_fetch_array ( $query_result, MYSQL_NUM )) {
			$this->errorCheck () ;
			return $row [ 0 ] ;
		} else {
			$this->errorCheck () ;
			return "" ;
		}
	}
	
	public function getAffectedRows () {
		$this->errorCheck () ;
		return $this->affected_rows ;
	}
	
	public function getInsertId () {
		$this->errorCheck () ;
		return $this->insert_id ;
	}
	
	public function query($query) {
         $query_result = mysql_query($query, $this->connection);
         $this->affected_rows = mysql_affected_rows($this->connection);
         $this->insert_id = mysql_insert_id($this->connection);
         $this->errorCheck();
      }
	
	/**
	 * selectDatabase
	 *
	 * This method selects the database identified by $database_name on  connection object
	 * property.  This method returns nothing.
	 *
	 * @param string $database_name The database name that should be connected to.
	 */
	public function selectDatabase($database_name) {
		mysql_select_db ( $database_name, $this->connection ) ;
		$this->errorCheck () ;
	}
}

class AdroitDBException extends Exception {
}
class AdroitDBConnectionException extends Exception {
}
?>
